<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="readerName">
        <el-input
          v-model="queryParams.readerName"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证号" prop="idNumber">
        <el-input
          v-model="queryParams.idNumber"
          placeholder="请输入身份证号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phoneNum">
        <el-input
          v-model="queryParams.phoneNum"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['reader:info:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['reader:info:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['reader:info:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['reader:info:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="姓名" align="center" prop="readerName"/>
      <el-table-column label="邮箱" align="center" prop="email"/>

      <el-table-column label="身份证号" align="center" prop="idNumber"/>
      <el-table-column label="手机号" align="center" prop="phoneNum"/>
      <el-table-column label="状态" align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['reader:info:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['reader:info:remove']"
          >删除
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-fold"
            v-hasPermi="['reader:info:record']"
            @click="viewBorrow(scope.row)"
          >借阅记录
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改读者信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="姓名" prop="readerName">
          <el-input v-model="form.readerName" placeholder="请输入姓名"/>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱"/>
        </el-form-item>
        <el-form-item label="身份证号" prop="idNumber">
          <el-input v-model="form.idNumber" placeholder="请输入身份证号"/>
        </el-form-item>
        <el-form-item label="手机号" prop="phoneNum">
          <el-input v-model="form.phoneNum" placeholder="请输入手机号"/>
        </el-form-item>
        <!--<el-form-item label="状态" prop="state">
          <el-input v-model="form.state" placeholder="请输入状态" />
        </el-form-item>-->
        <el-form-item label="状态" prop="state">
          <el-radio-group v-model="form.state">
            <el-radio
              v-for="dict in dict.type.sys_normal_disable"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}
            </el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <el-dialog
      title="借阅记录"
      :visible.sync="borrowVisible"
      width="80%">
      <el-table
        :data="borrowList"
        style="width: 100%">
        <el-table-column label="图书名称" align="left" prop="bookName"/>
        <el-table-column label="借阅开始时间" align="center" prop="borrowStartTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.borrowStartTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="借阅结束时间" align="center" prop="borrowEndTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.borrowEndTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="借阅天数" align="center" prop="dayNum"/>
        <el-table-column label="状态" align="center" prop="state">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.borrow_state" :value="scope.row.state"/>
          </template>
        </el-table-column>
        <el-table-column label="应付金额" align="center" prop="money"/>
      </el-table>

      <pagination
        v-show="borrowtotal>0"
        :total="borrowtotal"
        :page.sync="borrowParams.pageNum"
        :limit.sync="borrowParams.pageSize"
        @pagination="listBorrowList(readerId)"
      />
    </el-dialog>
  </div>
</template>

<script>
  import { listReader, getReader, delReader, addReader, updateReader } from '@/api/book/reader'
  import { listBorrow } from '@/api/book/borrow'

  export default {
    dicts: ['sys_normal_disable','borrow_state'],
    name: 'Info',
    data() {
      var idNum = (rule, value, callback) => {
        if ( value && (!(/\d{17}[\d|x]|\d{15}/).test (value) || (value.length !== 15 && value.length !== 18)) ) {
          callback (new Error ('身份证号码不符合规范'))
        } else {
          callback ()
        }
      }
      return {

        borrowList: [],
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 读者信息表格数据
        infoList: [],
        // 弹出层标题
        title: '',
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          readerName: null,
          email: null,
          state: null,
          idNumber: null,
          phoneNum: null,
          sex: null,
          createUser: null,
          updateUser: null
        },
        // 表单参数
        form: {
          state: '0'
        },
        borrowtotal:0,
        borrowVisible: false,
        borrowParams: {
          pageNum: 1,
          pageSize: 10,
          readerId: null,
          bookId: null,
          borrowStartTime: null,
          borrowEndTime: null,
          state: null,
          money: null,
          createUser: null,
          updateUser: null,
          returnTime: null
        },
        readerId:'',
        // 表单校验
        rules: {
          readerName: [
            { required: true, message: '读者名称不能为空', trigger: 'blur' }
          ],

          email: [
            { required: true, message: '邮箱地址不能为空', trigger: 'blur' },
            {
              type: 'email',
              message: '请输入正确的邮箱地址',
              trigger: ['blur', 'change']
            }
          ],
          phoneNum: [
            { required: true, message: '手机号码不能为空', trigger: 'blur' },

            {
              pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
              message: '请输入正确的手机号码',
              trigger: 'blur'
            }
          ],
          idNumber: [
            { required: true, message: '身份证号码不能为空', trigger: 'blur' },
            { validator: idNum, trigger: 'blur' }
          ]
        }
      }
    },
    created() {
      this.getList ()
    },
    methods: {
      /** 查询读者信息列表 */
      getList() {
        this.loading = true
        listReader (this.queryParams).then (response => {
          this.infoList = response.rows
          this.total = response.total
          this.loading = false
        })
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset ()
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          readerName: null,
          email: null,
          delFlag: null,
          state: '0',
          idNumber: null,
          phoneNum: null,
          sex: null,
          createTime: null,
          createUser: null,
          updateUser: null,
          updateTime: null
        }
        this.resetForm ('form')
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList ()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm ('queryForm')
        this.handleQuery ()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map (item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset ()
        this.open = true
        this.title = '添加读者信息'
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset ()
        const id = row.id || this.ids
        getReader (id).then (response => {
          this.form = response.data
          this.open = true
          this.title = '修改读者信息'
        })
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs[ 'form' ].validate (valid => {
          if ( valid ) {
            if ( this.form.id != null ) {
              updateReader (this.form).then (response => {
                this.$modal.msgSuccess ('修改成功')
                this.open = false
                this.getList ()
              })
            } else {
              addReader (this.form).then (response => {
                this.$modal.msgSuccess ('新增成功')
                this.open = false
                this.getList ()
              })
            }
          }
        })
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids
        this.$modal.confirm ('是否确认删除读者信息编号为"' + ids + '"的数据项？').then (function() {
          return delReader (ids)
        }).then (() => {
          this.getList ()
          this.$modal.msgSuccess ('删除成功')
        }).catch (() => {
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download ('book/info/export', {
          ... this.queryParams
        }, `reader_${new Date ().getTime ()}.xlsx`)
      },
      listBorrowList(readerId) {
        this.borrowParams.readerId = readerId
        this.loading = true
        listBorrow (this.borrowParams).then (response => {
          this.borrowList = response.rows
          this.borrowtotal = response.total
          this.loading = false
        })
      },
      viewBorrow(rowdata) {
        this.readerId = rowdata.id
        this.listBorrowList (rowdata.id)
        this.borrowVisible = true

      }

    }
  }
</script>
